package com.example.jbmsuppliers.sinopec.doorjurisd;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.IEGuardKey;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class IdentificationStepOneActivity extends ActivityUse {


    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.id_identification_edit_cardno)
    EditText idIdentificationEditCardno;
    @InjectView(R.id.id_text_ljty)
    TextView idTextLjty;

    private DialogLoading loading;
    public final static int Com_Add_Carno_key = 0001;
    public final static int EXETSSONT = 0002;

    @Override
    protected int setContentView1() {
        return R.layout.activity_identification_step_one;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        headerTitle.setText("开门认证");
        headerRight.setText("下一步");
    }





    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1: {
                    setResult(RESULT_OK);
                    finish();
                }
                break;
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        idIdentificationEditCardno.setText("");
    }



    protected void setAlertDialogqds(String mess) {
        new AlertDialog.Builder(this)
                .setMessage(mess)
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        NetWork netWork = new NetWork(mHandler);
                        netWork.setEditCardNo(idIdentificationEditCardno.getText().toString());
                        loading = new DialogLoading(IdentificationStepOneActivity.this, "验证中...", true);
                        loading.show();
                    }
                }).setNegativeButton("取消", null).show();
    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case Com_Add_Carno_key: {
                    IEGuardKey key = (IEGuardKey) msg.obj;
                    if (key.back) {
                        if (loading != null) {
                            loading.dismiss();
                        }
                        hint("验证成功", mHandler, EXETSSONT);
                    } else if (key.equals("time out")) {
                        if (loading != null) {
                            loading.dismiss();
                        }
                        hint(key.errorinfo);
                    } else {
                        if (loading != null) {
                            loading.dismiss();
                        }
                        hint(key.errorinfo);
                    }
                    break;
                }
                case EXETSSONT: {
                    finish();
                    break;
                }

            }


        }
    };

    @OnClick({R.id.header_right, R.id.header_back, R.id.id_text_ljty})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_right:
                String cardNo = idIdentificationEditCardno.getText().toString();
                if (cardNo.length() < 5) {
                    hint("请核实门禁卡NO号码！");
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(getBaseContext(), IdentificationStepTwoActivity.class);
                intent.putExtra("cardno", cardNo);
                startActivityForResult(intent, 1);
                break;
            case R.id.header_back:
                finish();
                break;
            case R.id.id_text_ljty:
                if (idIdentificationEditCardno.getText().toString().equals("")) {
                    hint("禁卡NO号码不能为空！");
                } else if (idIdentificationEditCardno.getText().toString().length() < 5) {
                    hint("请核实门禁卡NO号码！");
                } else {
                    setAlertDialogqds("体验期限为一个月，权限到期后将无法开门，请提前认证！");
                }
                break;
        }
    }
}
